package com.qingding.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVMobilePhoneVerifyCallback;
import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVSMS;
import com.avos.avoscloud.AVSMSOption;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.RequestMobileCodeCallback;
import com.avos.avoscloud.SaveCallback;
import com.avos.avoscloud.SignUpCallback;
import com.qingding.R;

import com.qingding.model.User;

import java.util.regex.Pattern;

public class RegisterActivity extends AppCompatActivity {

    private EditText etUsername;
    private EditText yanzhengma;
    private Button sendyanzhengma;
    private EditText etPassword;
    private Button btnRegister;
    private String[] strings1 = {"勤奋的","有好的","可爱的","坚强的","倔强的","善良的","自由的","美好的","骄傲的","伟大的"};
    private String[] strings2 = {"小白","小黑","大佬","企鹅","猫咪","火龙果","大枣","手机","天梯","轻咛","钰彬"};
    private int[] phonoId={R.drawable.photo1,R.drawable.photo2,R.drawable.photo3,R.drawable.photo4,R.drawable.photo5,R.drawable.photo6,
            R.drawable.photo7,R.drawable.photo8,R.drawable.photo9,R.drawable.photo10,R.drawable.photo11,R.drawable.photo12,R.drawable.photo13,R.drawable.photo14};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        //分别实例化三个编辑框，用户名、验证码和密码,实例化注册按钮和发送验证码按钮,同事设置密码框为不可编辑状态
        etUsername = (EditText) findViewById(R.id.re_username);
        yanzhengma = (EditText) findViewById(R.id.yanzhengma);
        etPassword = (EditText) findViewById(R.id.re_password);
        btnRegister = (Button) findViewById(R.id.regist);
        sendyanzhengma = (Button) findViewById(R.id.send_yanzhengma);
        sendyanzhengma.setEnabled(false);

        //实例化注册按钮、获取两个0~9的随机数,并为其分配随机的网名,
        int radom1 = (int) (Math.random()*9);
        int radom2 = (int) (Math.random()*9);
        int radom3 = (int) (Math.random()*13);
        final StringBuffer stringBuffer = new StringBuffer(strings1[radom1]);
        stringBuffer.append(strings2[radom2]);
        final int photoid = phonoId[radom3];


        //为发送验证码按钮注册事件监听器,发送验证码，发送成功则按钮变为不可编辑状态，同时密码框变为可编辑状态
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String userphone = etUsername.getText().toString();
                String password = etPassword.getText().toString();

                if (userphone.length() == 11 && (password.length()>6)){
                    AVUser user = new AVUser();
                    user.setUsername(stringBuffer.toString());
                    user.setPassword(password);
                    // 其他属性可以像其他AVObject对象一样使用put方法添加
                    user.put("mobilePhoneNumber", userphone);
                    user.put("PhotoId",photoid);
                    user.signUpInBackground(new SignUpCallback() {
                        public void done(AVException e) {
                            if (e == null) {
                                // successfully
                                SharedPreferences.Editor editor = getSharedPreferences("_User",0).edit();
                                editor.putString("userphonenumber",userphone);
                                editor.apply();
                                Toast.makeText(RegisterActivity.this, "验证码发送成功，请在验证码输入框内输入并验证", Toast.LENGTH_SHORT).show();
                                sendyanzhengma.setEnabled(true);
                            } else {
                                // failed
                                Toast.makeText(RegisterActivity.this, "注册失败，请输入正确的格式", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }else {
                    Toast.makeText(RegisterActivity.this, "请输入正确的手机号或密码小于6位", Toast.LENGTH_SHORT).show();
                }



            }
        });

        //为注册按钮添加事件监听器,看看验证码是否相匹配
        sendyanzhengma.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String yanzhengmatext = yanzhengma.getText().toString();
                if (yanzhengmatext.length() == 6 && (Pattern.compile("^\\d{6}$").matcher(yanzhengmatext).matches())){
                    AVUser.verifyMobilePhoneInBackground(yanzhengmatext, new AVMobilePhoneVerifyCallback() {
                        @Override
                        public void done(AVException e) {
                            if(e == null){
                                // 验证成功
                                Toast.makeText(RegisterActivity.this, "验证成功，请登录", Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
                            } else {
                                Log.d("SMS", "Verified failed!");
                                Toast.makeText(RegisterActivity.this, "验证失败，但以注册成功", Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
                            }
                        }
                    });
                }


            }
        });
    }



}
